require "Common/define"
require "Logic/Config"
require "Common/functions"

UIClimbTowerRecord = {};
HelperFunc.SetUIEnv(UIClimbTowerRecord)
local this = UIClimbTowerRecord;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;
this.hasInit = false

this.level = nil;
this.dynamicList = nil;
this.btn_back = nil;

this.towerLevel = 0;

function UIClimbTowerRecord.Init(level)
    curTowerlevel = level;
    local name, packageName = getUIName(PanelNames.UIClimbTowerRecord);
    panelMgr:Push(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIClimbTowerRecord.onCreate(obj)
    if not hasInit then
        hasInit = true;

        title = obj:GetChild("title")
        level = obj:GetChild("ranking")
        dynamicList = obj:GetChild("list")
        btn_back = obj:GetChild("btn_back");
        bgClose = obj:GetChild("bgClose");
        this.nothingController = obj:GetController("nothing");

        this.addEvent();
    end

    this.initUI();
    if logicMgr.TowerManager.getClimbTowerRecordData() == nil then
        local towerType = logicMgr.TowerManager.getClimbType(this.towerType)
        towerHandler.sendPassRecord(curTowerlevel, towerType);
    end
end

function UIClimbTowerRecord.addEvent()
    btn_back.onClick:Add(this.onClickBack);
    bgClose.onClick:Add(this.onClickBack);
    ListenEvent(Config.EventType.Fresh_TowerRecord, this.refreshWithData)
end

function UIClimbTowerRecord.removeEvent()
    btn_back.onClick:Remove(this.onClickBack);
    bgClose.onClick:Remove(this.onClickBack);
    CloseEvent(Config.EventType.Fresh_TowerRecord, this.refreshWithData)
end

function UIClimbTowerRecord.OnDestroy()
    this.removeEvent();
end

function UIClimbTowerRecord.initUI()
    title.title = getLanguage("Sys_TowerBtn_Record");
    level.text = string.format(getLanguage("Sys_TowerLevel"), tonumber(curTowerlevel));
    dynamicList:RemoveChildrenToPool();
    this.refreshWithData();
end

function UIClimbTowerRecord.refreshWithData()
    local recordData = logicMgr.TowerManager.getClimbTowerRecordData();
    if recordData == nil then
        return ;
    end
    local playerDataList = recordData["playerDataList"];
    if playerDataList == nil or #playerDataList == 0 then
        this.nothingController.selectedIndex = 1;
    else
        this.nothingController.selectedIndex = 0;
    end
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.TowerRecordItemList);
    if playerDataList ~= nil then
        for i = 1, #playerDataList do
            local itemobj = dynamicList:AddItemFromPool()
            if ctrlItem ~= nil then
                ctrlItem.initData(itemobj, playerDataList[i]);
                itemobj:GetChild("btn_skada").onClick:Add(function()
                    showUI(PanelNames.UILevelRecode, EnumConst.FightTypeConst.TOWER, playerDataList[i]["rawRecord"],
                            function()
                                UIClimbTower.closeEnemyFormation();
                            end,
                            function()
                                showUI(PanelNames.UIClimbTowerRecord, curTowerlevel)
                            end);
                end)
                itemobj:GetChild("btn_player").onClick:Add(function()
                    local record = playerDataList[i]["rawRecord"];
                    if record.version == GetFightVersion() then

                        BattlePlaybackManager.setBattleRecord(playerDataList[i]["rawRecord"]);
                        BattlePlaybackManager.setBattleResult(1);
                        BattlePlaybackManager.requestFightCmdThenPlay(playerDataList[i]["rawRecord"], EnumConst.FightTypeConst.TOWER,
                                function()
                                    FightManager.SetRePlay(true);
                                    UIClimbTower.closeEnemyFormation();
                                end, function()
                                    local curTowerType = logicMgr.TowerManager.getClimbType();
                                    --showUI(PanelNames.UIClimbTower,nil,false,curTowerType);
                                    showUI(PanelNames.UIClimbTowerRecord, curTowerlevel);
                                end);
                    else
                        UISysTips.AddMsg(getLanguage("ErrorCode_220005"))
                    end
                end)
            end
        end
    end
end

function UIClimbTowerRecord.ClimbItemRender(index, itemobj)
    local heroData = playerDataList[index + 1];
    -- local heroData = decodeJsonStrToTabel(datasource);
    local go = itemobj;

    local ctrlItem = CtrlManager.GetCtrl(PanelNames.TowerRecordItemList);
    if ctrlItem ~= nil then
        ctrlItem.initData(go, heroData);
    end
end

function UIClimbTowerRecord.onClickBack()
    logicMgr.TowerManager.setClimbTowerRecordData()
    closeUI(PanelNames.UIClimbTowerRecord);
end

return UIClimbTowerRecord;